package com.youko.backstage.service;

import com.youko.backstage.bean.commodity.CommoditySku;
import com.youko.backstage.bean.order.OrderSku;
import com.youko.backstage.dto.order.OrderSkuDto;
import com.youko.backstage.mapper.commodity.CommoditySkuMapper;
import com.youko.backstage.mapper.commodity.OrderSkuMapper;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: youko
 * @classname: OrderSkuService
 * @Description: some desc
 * @date: 2022/7/8 12:02
 */

@Service
public class OrderSkuService {
    @Autowired
    OrderSkuMapper orderSkuMapper;

    @Autowired
    CommoditySkuMapper commoditySkuMapper;


    /**
     * 根据商家id来查所有sku，再查所有订单
     */
    public List<OrderSkuDto> selectAllOrderSkuByMerchantId(int merchantId){
        List<CommoditySku> commoditySkus=commoditySkuMapper.selectAllSkuByMerchantId(merchantId);
        List<OrderSku> orderSkus=new ArrayList<OrderSku>();
        for (CommoditySku cs:commoditySkus){
            orderSkus.addAll(orderSkuMapper.selectAllOrderSkuBySkuId(cs.getId()));
        }
        List<OrderSkuDto> orderSkuDtos=new ArrayList<OrderSkuDto>();
        for (OrderSku os:orderSkus){
            OrderSkuDto orderSkuDto=new OrderSkuDto();
            orderSkuDto.setOrderId(os.getOrderId());
            orderSkuDto.setCommoditySku(commoditySkuMapper.selectSku(os.getSkuId()));
            orderSkuDto.setNum(os.getNum());
            orderSkuDto.setIsSend(os.getIsSend());
            orderSkuDtos.add(orderSkuDto);
        }
        return  orderSkuDtos;
    }

    /**
     * 根据订单id更改发货状态实现发货
     */
    public void updateSendStatusById(int id){
        orderSkuMapper.updateStatusById(id);
    }
}
